<?php
class YumAuthServer {
	
	protected $hashcode;
	protected $loginForm;
	
	public function __construct() {		
		$this->request = Yii::app ()->getRequest ();
	}
	
	public function login($data = '') {
		$result = array();
		$this->loginForm = new YumLoginForm;
		if(isset($data['username']) && isset($data['password'])) {
			$this->loginForm->attributes = $data;
			if($this->loginForm->validate() && $user=$this->loginForm->login()){
				$user->lastvisit = time();
				$user->save(true, array('lastvisit'));
				
				YumActivityController::logActivity($user, YumActivityLog::LOG_LOGIN, 'User {username} successfully logged in from API', json_encode( array('{username}' => $user->username)));
				
				$result = $user->attributes;				
				$result['email'] = $user->profile->email;
				
				$data = array('value'=>$result, 'valueDefault'=>serialize('authSSO'), 'name'=>'authSSO');
				
				return json_encode($data);
			}
			else {
				$result['errors'] = $this->loginForm->errors;
			}
		}
		return $result;
	}
	
	public function loginSSO($data = '') {
		$session = Util::getSession ();
		$user = Yii::app ()->user;
		if (! Yii::app ()->user->isGuest) {
			$usrID = $user->getId ();
			$token = $session->get ( 'access_token' );
			if (empty ( $token )) {
				/* exprired 5 minute */
				$accessToken = Util::encrypt ( time() . Yii::app ()->params ['key'] . md5 ( $usrID ) );
				$token = Util::accessToken ( true, $accessToken );
			}
			$conj = (count(explode("?", $data ['redirect_url'])) > 1) ? "&" : "?";
			$this->request->redirect ( $data ['redirect_url'] . $conj . 'auth_token=' . $token );
		} else {
			Util::clearToken ();
			$this->request->redirect ( '/users/auth/login?redirect_url=' . $data ['redirect_url'] );
		}
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	public function getProfile($data = '') {
		$session = Util::getSession ();
		$auth_token = $session->get ( 'access_token' );
		$user = Yii::app ()->user;
		$accessToken = Util::decrypt ( $data ['token'] );
		$arrToken = explode ( Yii::app ()->params ['key'], $accessToken );		
		$obj = YumUser::model ()->find ( 'MD5(id) = :id', array (':id' => $arrToken[1] ) );
		if(!empty($obj) && time() - $arrToken[0] < 300){//5 minutes
			if(!empty($obj->id)){
				$result = $obj->attributes;
				return $result;
			}
		}
		return false;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	public function searchUser($data = '') {
		$word = $data ['word'];
		
		if($data['searchflag']==1){
			/** Search 1 result **/
			$criteria = new CDbCriteria;
			$criteria->condition = 'username LIKE :word';
			$criteria->params=array(':word'=>$word);
			$obj = YumUser::model()->find($criteria);
			if(isset($obj)) {
				$result = $obj->attributes;		
				$result['screenname'] = $obj->profile->fullname;		
				$result['birthday'] = $obj->profile->birthday;		
				$result['gender'] = $obj->profile->gender;		
				$result['email'] = $obj->profile->email;
			}
			else {
				$result = YumUser::model()->attributes;
 			}
		}else{
			/** Search multi result **/
			$criteria = new CDbCriteria;
			$criteria->condition = 'username LIKE :word';
			$criteria->join = 'INNER JOIN profile p ON t.id = p.user_id';
			$criteria->params=array(':word'=> $word.'%');
			$obj = YumUser::model()->findAll($criteria);
			$result = array();
			if($obj){
				$i=0;
				foreach ($obj as $item){
					$result[$i] = $item->attributes;		
					$result[$i]['screenname'] = $item->profile->fullname;		
					$result[$i]['birthday'] = $item->profile->birthday;		
					$result[$i]['gender'] = $item->profile->gender;		
					$result[$i]['email'] = $item->profile->email;	
					$i++;				
				}
			}
		}		
		return $result;	
	}
	
	public function getInviter($data = '') {
		Yii::import ( 'backend.modules.profile.models.*' );
		if(!empty($data)){		
			$profile = YumProfile::model()->find("email = :email", array(':email' => $data['email']));
		}		
		if(!empty($profile)){
			$inviter = YumUserInvite::model()->find('rcpt_id = :rcpt_id', array(':rcpt_id' => $profile->user_id));
			if(isset($inviter)) {
				$inviter = $inviter->inviter; 
				$i = $inviter->attributes; 
				return $inviter->attributes;
			}
		}
		return false;		
	}
	
	
		
}